package tests.common.control;

import java.io.File;

import junit.framework.TestCase;
import harima.agameditor.common.control.AwesomeEditorEnviroment;
import harima.agameditor.common.control.Project;

public class AwesomeEditorEnvyromentFixture extends TestCase{

	String pathSeparator = File.separator;
	
	public void test_CanSetRootPath() throws Exception {
		String path = "/SomePath";
		AwesomeEditorEnviroment.setRootPath(path);
		assertEquals(path, AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.ROOT_PATH));
	}
	
	@Override
	protected void tearDown() throws Exception {
		assertTrue(AwesomeEditorEnviroment.deleteCurrentProject());
	}
	
	public void test_GetsPathRelativeToRoot_GetPathRequested_Always() throws Exception {
		String path = "SomePath";
		AwesomeEditorEnviroment.setRootPath(path);
		String mapsPath = AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.MAPS_PATH);
		assertNotNull(mapsPath);
		assertTrue(mapsPath.startsWith(path));
	}
	
	public void test_ReturnsNull_GetPathNameRequested_InvalidPathName() throws Exception {
		String path = AwesomeEditorEnviroment.getPath("some random path name");
		assertNull(path);
	}
	
	public void test_CreatesRootProjectFolder_CreateProjectRequested_Always() throws Exception {
		String rootFolder = "myProject";
		Project project = new Project(rootFolder);
		boolean projectCreated = AwesomeEditorEnviroment.createProject(project);
		assertTrue(projectCreated);
	}
	
	public void test_SetsPassedProjectAsCurrentProject_CreateProjectRequested_Always() throws Exception {
		Project project = new Project("myPath");
		AwesomeEditorEnviroment.createProject(project);
		assertNotNull(AwesomeEditorEnviroment.getCurrentProject());
		assertSame(project, AwesomeEditorEnviroment.getCurrentProject());
	}
	
	public void test_SetsProjectRootPathAsRootPath_CreateProjectRequested_Always() throws Exception {
		Project project = new Project("SomeProject");
		AwesomeEditorEnviroment.createProject(project);
		assertEquals("SomeProject", 
				AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.ROOT_PATH));
	}
	
	public void test_CanDeleteCurrentProject_DeleteProjectRequested_Always() throws Exception {
		Project project = new Project("SomeProject");
		assertTrue(AwesomeEditorEnviroment.createProject(project));
		assertTrue(AwesomeEditorEnviroment.deleteCurrentProject());
		assertFalse(new File("SomeProject").exists());
	}

	public void test_ClearsEnvyroment_DeleteProjectRequested_Always() throws Exception {
		Project project = new Project("SomeProject");
		AwesomeEditorEnviroment.createProject(project);
		assertTrue(AwesomeEditorEnviroment.deleteCurrentProject());
		assertNull(AwesomeEditorEnviroment.getCurrentProject());
		assertNull(AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.ROOT_PATH));
	}
	
	public void test_HandlesNullPointer_DeleteProjectRequested_CurrentProjectIsNull() throws Exception {
		AwesomeEditorEnviroment.clearEnvyroment();
		assertTrue(AwesomeEditorEnviroment.deleteCurrentProject());
	}
	
	public void test_DoesNotSetCurrentPath_CreateProjectRequested_CannotCreateProject() throws Exception {
		Project myProject = new Project("!n/valid/path");
		assertFalse(AwesomeEditorEnviroment.createProject(myProject));
		assertNull(AwesomeEditorEnviroment.getCurrentProject());
		assertNull(AwesomeEditorEnviroment.getPath(AwesomeEditorEnviroment.ROOT_PATH));
	}
	
	public void test_CanCreateWholeProjectGyerachy_CreateProjectRequested_always() throws Exception {
		String rootDir = "myProject";
		Project someProject = new Project(rootDir);
		assertTrue(AwesomeEditorEnviroment.createProject(someProject));
		assertDirecoty(rootDir);
		
		assertDirecoty(rootDir + pathSeparator + "maps");
		
		assertDirecoty(rootDir + pathSeparator + "code");
		assertDirecoty(rootDir + pathSeparator + "code" + pathSeparator + "map_behaivors");
		assertDirecoty(rootDir + pathSeparator + "code" + pathSeparator + "map_objects");
		
		
		assertDirecoty(rootDir + pathSeparator + "resources");
		assertDirecoty(rootDir + pathSeparator + "resources" + pathSeparator + "images");
		assertDirecoty(rootDir + pathSeparator + "resources" + pathSeparator + "tiles");
		assertDirecoty(rootDir + pathSeparator + "resources" + pathSeparator + "sounds");
		assertDirecoty(rootDir + pathSeparator + "resources" + pathSeparator + "sprites");
	}

	private void assertDirecoty(String path) {
		File dir = new File(path);
		assertTrue(dir.exists());
		assertTrue(dir.isDirectory());
	}
}
